package cn.newhopedairy.zmd.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @author Neo.Li
 * @date 2021-01-1416:13
 */
@Slf4j
@RestController
public class TestOauth2Controller {
    @GetMapping("/")
    public String index(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,
                        @AuthenticationPrincipal OAuth2User oauth2User) {
        return "username:" + oauth2User.getName() + "\n"
                + "clientname:" + authorizedClient.getClientRegistration().getClientName() + "\n"
                + "属性:" + oauth2User.getAttributes();
    }

    @GetMapping("/whoami")
    public Authentication me(Authentication authentication) {
        return authentication;
    }

    @GetMapping("/info")
    public Principal info(Principal principal) {
        return principal;
    }
}
